package com.xxl.job.consume.repository;

import com.xxl.job.consume.common.RedisKeyConstant;
import com.xxl.job.consume.model.*;
import com.xxl.job.consume.utils.JSONUtils;
import com.xxl.job.core.log.XxlJobLogger;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.IOException;

/**
 * 查询实时数据
 */
@Repository
public class CurrentInfoRepository {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询环控系统
     * @param devCode
     * @param signalCode
     * @return
     */
    public CurrentInfoHkModel queryHkCurrent(String devCode, String signalCode) {
        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.HK_CURRENT + devCode);
        String currentSignalMessageJson = hashOps.get(signalCode);
        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
            try {
                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoHkModel.class);
            } catch (IOException e) {
                XxlJobLogger.log("环控系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
            }
        }
        return null;
    }

//    /**
//     * 查询电话系统
//     * @param devCode
//     * @param signalCode
//     * @return
//     */
//    public CurrentInfoDhModel queryDhCurrent(String devCode, String signalCode) {
//        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.DH_CURRENT + devCode);
//        String currentSignalMessageJson = hashOps.get(signalCode);
//        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
//            try {
//                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoDhModel.class);
//            } catch (IOException e) {
//                XxlJobLogger.log("电话系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
//            }
//        }
//        return null;
//    }
//
//
//    /**
//     * 查询入侵系统
//     * @param devCode
//     * @param signalCode
//     * @return
//     */
//    public CurrentInfoRqModel queryRqCurrent(String devCode, String signalCode) {
//        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.RQ_CURRENT + devCode);
//        String currentSignalMessageJson = hashOps.get(signalCode);
//        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
//            try {
//                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoRqModel.class);
//            } catch (IOException e) {
//                XxlJobLogger.log("入侵系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
//            }
//        }
//        return null;
//    }
//
//
//    /**
//     * 查询消防系统
//     * @param devCode
//     * @param signalCode
//     * @return
//     */
//    public CurrentInfoXfModel queryXfCurrent(String devCode, String signalCode) {
//        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.XF_CURRENT + devCode);
//        String currentSignalMessageJson = hashOps.get(signalCode);
//        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
//            try {
//                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoXfModel.class);
//            } catch (IOException e) {
//                XxlJobLogger.log("自控系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
//            }
//        }
//        return null;
//    }
//
//    /**
//     * 查询AP系统
//     * @param devCode
//     * @param signalCode
//     * @return
//     */
//    public CurrentInfoApModel queryApCurrent(String devCode, String signalCode) {
//        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.AP_CURRENT + devCode);
//        String currentSignalMessageJson = hashOps.get(signalCode);
//        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
//            try {
//                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoApModel.class);
//            } catch (IOException e) {
//                XxlJobLogger.log("自控系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
//            }
//        }
//        return null;
//    }
//
//    /**
//     * 查询采集口系统
//     * @param devCode
//     * @param signalCode
//     * @return
//     */
//    public CurrentInfoGatherModel queryGatherCurrent(String devCode, String signalCode) {
//        BoundHashOperations<String, String, String> hashOps = redisTemplate.boundHashOps(RedisKeyConstant.GATHER_CURRENT + devCode);
//        String currentSignalMessageJson = hashOps.get(signalCode);
//        if (StringUtils.isNotBlank(currentSignalMessageJson)) {
//            try {
//                return  JSONUtils.json2pojo(currentSignalMessageJson, CurrentInfoGatherModel.class);
//            } catch (IOException e) {
//                XxlJobLogger.log("自控系统：终端JSON='{}' 转换异常！", currentSignalMessageJson, e);
//            }
//        }
//        return null;
//    }


    /**
     * 保存环控系统
     * @param currentInfoHkModel
     */
    public void save(CurrentInfoHkModel currentInfoHkModel) {
        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.HK_CURRENT+currentInfoHkModel.getDevCode());
        valueOperations.put(currentInfoHkModel.getSignalCode(),
                currentInfoHkModel.toJson());
    }
//
//    /**
//     * 保存电话系统
//     * @param currentInfoDhModel
//     */
//    public void save(CurrentInfoDhModel currentInfoDhModel) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.DH_CURRENT+currentInfoDhModel.getDevCode());
//        valueOperations.put(currentInfoDhModel.getSignalCode(),
//                currentInfoDhModel.toJson());
//    }
//
//    /**
//     * 保存入侵系统
//     * @param currentInfoRqModel
//     */
//    public void save(CurrentInfoRqModel currentInfoRqModel) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.RQ_CURRENT+currentInfoRqModel.getDevCode());
//        valueOperations.put(currentInfoRqModel.getSignalCode(),
//                currentInfoRqModel.toJson());
//    }
//
//    /**
//     * 保存消防系统
//     * @param currentInfoXfModel
//     */
//    public void save(CurrentInfoXfModel currentInfoXfModel) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.XF_CURRENT+currentInfoXfModel.getDevCode());
//        valueOperations.put(currentInfoXfModel.getSignalCode(),
//                currentInfoXfModel.toJson());
//    }
//
//    /**
//     * 保存采集口系统
//     * @param currentInfoApModel
//     */
//    public void save(CurrentInfoApModel currentInfoApModel) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.AP_CURRENT+currentInfoApModel.getDevCode());
//        valueOperations.put(currentInfoApModel.getSignalCode(),
//                currentInfoApModel.toJson());
//    }
//
//    /**
//     * 保存采集口系统
//     * @param currentInfoGatherModel
//     */
//    public void save(CurrentInfoGatherModel currentInfoGatherModel) {
//        BoundHashOperations<String, String, String> valueOperations = redisTemplate.boundHashOps(RedisKeyConstant.GATHER_CURRENT+currentInfoGatherModel.getDevCode());
//        valueOperations.put(currentInfoGatherModel.getSignalCode(),
//                currentInfoGatherModel.toJson());
//    }

}
